package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.att.intern.webservice.Incident;

/*
 * GLCResultOpenActivity
 * This class is for the GLC Search Results with "OPEN" transaction
 */
public class GLCResultOpenActivity extends Activity {

	private String[] items = new String [5];
	// ArrayList of building Incidents
	private ArrayList<Incident> ListOfIncidents;

	// UI elements
	private TextView headerTextView;
	private TextView subheaderTextView;

	private LinearLayout headerForm;

	private ListView openListView;

	private String[] buildingName;

	// Adapter to create listView
	private ArrayAdapter<String> adapter;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_glcresult_open);
		
		final String user = this.getIntent().getStringExtra("USERNAME");
		final String authorizationType = this.getIntent().getStringExtra("AUTHORIZATIONTYPE");
		final String sessionToken = this.getIntent().getStringExtra("SessionToken");
		//picture
		final String picture = this.getIntent().getStringExtra("PictureFile");
		
		Log.v("OpenResults", "Displaying UI elements");
		// Display UI elements
		headerTextView = (TextView) findViewById(R.id.headerTextView);
		subheaderTextView = (TextView) findViewById(R.id.subheaderTextView);

		headerForm = (LinearLayout) findViewById(R.id.HeadersForm);

		// Retrieve ArrayList of Incidents
		Log.v("Open Results", "Retrieveing ArrayList of Incidents from intent");
		ListOfIncidents = (ArrayList<Incident>) this.getIntent()
				.getSerializableExtra("GLC_RESULTS");
		Log.v("Open Results", "Retrieved ArrayList of Incidents from intent");

		// Placing BuildingName for each location in String Array
		buildingName = new String[ListOfIncidents.size()];
		for (int i = 0; i < ListOfIncidents.size(); i++) {
			buildingName[i] = (ListOfIncidents.get(i).getBuildingName());
		}
		Log.v("Open Results", "Took RecNum and placed that in String [] Rec");

		// Intializing ArrayAdapter for creation of List
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, buildingName);

		openListView = (ListView) findViewById(R.id.openListView);
		openListView.setAdapter(adapter);

		Log.d("DialogBox", "Creation onItemClickListner");
		openListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
					long arg3) {
				intializeArray(arg2);
				// TODO Auto-generated method stub
				Log.d("DialogBox", "Created OnItemClick");
				AlertDialog.Builder display = new AlertDialog.Builder(GLCResultOpenActivity.this);
				Log.v("DialogBox", "Attempting to create dialog");
				display.setTitle(ListOfIncidents.get(arg2).getBuildingName())
						.setItems(items, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								// do nothing here
							}// End of onClick
						}// End of OnClickListner
						)// End Of setItems
							// Update Button, Launches new activity and then
							// closes dialog
						.setItems(items, new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								
							}
						})
						.setPositiveButton(R.string.Update,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										// Launch Update
										//Intent i = new Intent(this, UpdateActivity.class);
										Intent i = new Intent(getBaseContext(), UpdateAssementActivity.class);
										/*i.setClassName(
												this,
												"UpdateActivity");*/
										i.putExtra("SESSION", sessionToken);
										i.putExtra("USER", user);
										i.putExtra("AUTHTYPE", authorizationType);
										i.putExtra("INCIDENT", ListOfIncidents.get(arg2));
										i.putExtra("PictureFile", picture);
										Log.d("GLCResultOpen", "Got the incident!");
										i.putExtra("RECORDID", ListOfIncidents.get(arg2).getRecNumber());
										Log.d("GLCResultOpen", "Record Number: "+ListOfIncidents.get(arg2).getRecNumber());
										startActivity(i);
										dialog.cancel();
									}// End of onClick
								})// End of Set positive Button
									// Exit Button, closes dialog Box
						.setNegativeButton(R.string.Exit,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) { //
										// TODO Auto-generated method stub
										// DisplayedInfoDialog.this.getDialog().cancel();
										// } });
									}// End of onClick
								}// End of onClicklistner
						);// End of setNegative button and creation of
							// alertDialog

				Log.v("DialogBox", "Attempting to display");
				// Show Dialog box
				AlertDialog displayInfo = display.create();
				displayInfo.show();
			}
		});// End of onItemClick for list items
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.glcresult_open, menu);
		return true;
	}

	public void intializeArray(int arg2) {
		
		Log.d("IntializeArray", "Attempting to intialize Array");
		Log.d("IntializeArray", "arg2: "+ arg2);
		// Used to display hold text
		String RecNum;
		String glc;
		// No hurricane found
		String hurricane;
		String status;
		String wr;

		// Used to display text
		RecNum = Integer.toString(ListOfIncidents.get(arg2).getRecNumber());
		glc = Integer.toString(ListOfIncidents.get(arg2).getGeoLoc());
		hurricane = ListOfIncidents.get(arg2).getEventName();
		status = ListOfIncidents.get(arg2).getIncidentStatus();
		wr = ListOfIncidents.get(arg2).getWorkReqNumber();
		// Used to display text
		items[0] = "Record Number: " + RecNum;
		items[1] = "GLC: " + glc;
		items[2] = "Event Name: " + hurricane;
		items[3] = "Status: " + status;
		items[4] = "Work Record Number: " + wr;
		Log.d("Done", "Attempting to intialize Array");
	}

}
